<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ajax Post 请求</title>

    <!-- 设置div的样式 -->
    <style>
        #result {
            width: 200px;
            height: 200px;
            border: solid 5px olive;
        }
    </style>
</head>

<body>
    <div id="result"></div>

    <script>
        const div = document.getElementById('result');
        // 监听鼠标移出事件
        div.addEventListener('mouseover', () => {
            const xhr = new XMLHttpRequest();
            xhr.open('post', 'http://127.0.0.1:9999/server');

            // 设置预定义请求头:请求内容方式
            xhr.setRequestHeader('Content-Type', 'application/x-www-from-urlencoded');
            // 设置非预定义请求头:需要服务端做出设置，否则请求报错
            xhr.setRequestHeader('name', 'xiaoxia');

            xhr.send('a=1&b=2&c=3');
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status >= 200 && xhr.status < 300) {
                        div.innerHTML = xhr.response;
                    }
                }
            }
        })
    </script>
</body>

</html>